package com.bdmc.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.bo.DevFacilityRealtimeBo;
import com.bdmc.device.domain.vo.DevFacilityRealtimeVo;
import com.bdmc.device.domain.DevFacilityRealtime;
import com.bdmc.device.mapper.DevFacilityRealtimeMapper;
import com.bdmc.device.service.IDevFacilityRealtimeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 满溢设备实时数据Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-22
 */
@Service
public class DevFacilityRealtimeServiceImpl extends ServicePlusImpl<DevFacilityRealtimeMapper, DevFacilityRealtime, DevFacilityRealtimeVo> implements IDevFacilityRealtimeService {

    @Override
    public DevFacilityRealtimeVo queryById(Integer realtimeId){
        return getVoById(realtimeId);
    }

    @Override
    public TableDataInfo<DevFacilityRealtimeVo> queryPageList(DevFacilityRealtimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevFacilityRealtime> lqw = buildQueryWrapper(bo);
        Page<DevFacilityRealtimeVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DevFacilityRealtimeVo> mySelectPage(DevFacilityRealtimeBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<DevFacilityRealtimeVo> queryList(DevFacilityRealtimeBo bo) {
        LambdaQueryWrapper<DevFacilityRealtime> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<DevFacilityRealtime> buildQueryWrapper(DevFacilityRealtimeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DevFacilityRealtime> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeviceId() != null, DevFacilityRealtime::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getOnlineStatus() != null, DevFacilityRealtime::getOnlineStatus, bo.getOnlineStatus());
        lqw.eq(bo.getSign() != null, DevFacilityRealtime::getSign, bo.getSign());
        lqw.eq(bo.getOverflow() != null, DevFacilityRealtime::getOverflow, bo.getOverflow());
        lqw.eq(bo.getFire() != null, DevFacilityRealtime::getFire, bo.getFire());
        return lqw;
    }

    @Override
    public Boolean insertByBo(DevFacilityRealtimeBo bo) {
        DevFacilityRealtime add = BeanUtil.toBean(bo, DevFacilityRealtime.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setRealtimeId(add.getRealtimeId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(DevFacilityRealtimeBo bo) {
        DevFacilityRealtime update = BeanUtil.toBean(bo, DevFacilityRealtime.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DevFacilityRealtime entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public List<DevFacilityRealtimeVo> selectDevFacilityRealtimeList(DevFacilityRealtimeBo facilityRealtime) {
        return baseMapper.selectDevFacilityRealtimeList(facilityRealtime);
    }

    @Override
    public Map<String, Object> countHomeFacStatus() {
        return baseMapper.countHomeFacStatus();
    }
}
